package at.fhooe.mc.bluetooth;

import java.io.DataInputStream;
import java.io.IOException;

import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;

import net.rim.device.api.bluetooth.BluetoothSerialPort;
import net.rim.device.api.bluetooth.BluetoothSerialPortInfo;

public class Bluetooth {

	public static StreamConnection bluetoothConnection = null;
	private static DataInputStream reader = null;
	
	public static boolean open(){
		if(!isOpenend()){
			BluetoothSerialPortInfo[] info = BluetoothSerialPort.getSerialPortInfo();
			try {
				bluetoothConnection = (StreamConnection)Connector.open(info[0].toString(),
						Connector.READ);
				return true;
			} catch (IOException e) {
				e.printStackTrace();
				return false;
			}
		}
		
		return true;
	}
	
	public static boolean isOpenend(){
		return bluetoothConnection != null;
	}
	
	public static boolean close(){
		if (bluetoothConnection != null) {
			try {
			     bluetoothConnection.close();
			     return true;
			    } catch(IOException ioe) {
			    	return false;
			      }
		}

		return true;
	}
}
